package cn.tedu.adminsystem.webapi.service.impl;

import cn.tedu.adminsystem.service.PhotoUploadService;
import cn.tedu.adminsystem.service.SlideshowService;
import cn.tedu.adminsystem.webapi.mapper.SlideshowMapper;
import cn.tedu.adminsystem.webapi.security.CurrentAdmin;
import cn.tedu.community.commons.excepion.ServiceException;
import cn.tedu.community.commons.pojo.slideshow.entity.Slideshow;
import cn.tedu.community.commons.pojo.slideshow.vo.SlideshowList;
import cn.tedu.community.commons.restful.ServiceCode;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
@Slf4j
@DubboService
public class SlideshowImpl implements SlideshowService {

    @Autowired
    private PhotoUploadService photoUploadService;

    @Autowired
    private SlideshowMapper mapper;

    @Override
    public void addNew(String photo) {
        Long adminId = CurrentAdmin.getAdminId();
        Slideshow slideshow = new Slideshow();
        slideshow.setAdminId(adminId);
        slideshow.setPhoto(photo);
        int addNew = mapper.addNew(slideshow);
        if (addNew!=1){
            throw new ServiceException(ServiceCode.ERR_INSERT,"服务器繁忙，请稍后再试！");
        }
    }

    @Override
    public void deleteById(Long id) {
        String photo = mapper.selectPhotoById(id);
        if (photo==null){
            throw new ServiceException(ServiceCode.ERR_NOTFOUND,"轮播图已不存在！");
        }
        int deleteById = mapper.deleteById(id);
        if (deleteById!=1){
            throw new ServiceException(ServiceCode.ERR_DELETE,"服务器繁忙，请稍后再试！");
        }
        photoUploadService.deletePhoto(photo);
    }

    @Override
    public List<SlideshowList> getSlideshowList() {
        List<SlideshowList> slideshowLists = mapper.slideshowList();
        if (slideshowLists.isEmpty()){
            throw new ServiceException(ServiceCode.ERR_NOTFOUND,"暂无轮播图！");
        }
        return slideshowLists;
    }
}
